package com.mall507.fivepeaks.activity.community;

import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hzh.frame.comn.annotation.ViewInject;
import com.hzh.frame.ui.activity.AbsRecyclerViewUI;
import com.hzh.frame.widget.xrecyclerview.RecyclerViewHolder;
import com.hzh.frame.widget.xrefresh.XSwipeRefreshLayout;
import com.mall507.comn.model.Problem;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.activity.main.MainTourRUI;
import com.mall507.fivepeaks.widget.imageview.CircleImageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 有问必答
 * @date 2019/8/8
 */
@Route(path = "/community/CommunityProblemRUI")
public class CommunityProblemRUI extends AbsRecyclerViewUI<Problem> {

    @Override
    protected int setLayoutId() {
        return R.layout.ui_rv_community_problem;
    }

    @Override
    protected void bindView() {
        showLoding();
        getTitleView().setContent("有问必答");
        getTitleView().setRightContent("发布");
        getTitleView().setRightOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance()
                        .build("/community/CommunityProblemReleaseUI")
                        .navigation();
            }
        });
    }

    @Override
    protected String setHttpPath() {
        return "Community/getQuestion";
    }
    
    @Override
    protected List<Problem> handleHttpData(JSONObject response) {
        List<Problem> list = new ArrayList<>();
        if (200 == response.optInt("code")) {
            JSONArray datas = response.optJSONArray("data");
            for (int i = 0; i < datas.length(); i++) {
                JSONObject object = datas.optJSONObject(i);
                JSONObject member = datas.optJSONObject(i).optJSONObject("member");
                Problem model = new Problem();
                model.setNid(object.optString("id"));
                model.setName(object.optString("title"));
                if (member != null && member.length()>0){
                    model.setUserHead(member.optString("head_img"));
                    model.setUserName(member.optString("nickname"));
                }
                model.setUserTime(object.optString("create_at"));
                model.setBrowsingNum(object.optString("views"));
                list.add(model);
            }
        }
        dismissLoding();
        return list;
    }

    @Override
    protected int setItemLayoutId(int viewType) {
        return R.layout.item_rv_community_problem;
    }

    @Override
    protected void bindItemData(RecyclerViewHolder holder, int position, final Problem model) {
        ((CircleImageView) holder.getView(R.id.head)).setImageURI(model.getUserHead());
        holder.setText(R.id.name, model.getName());
        holder.setText(R.id.nice, model.getUserName());
        holder.setText(R.id.time, model.getUserTime());
        holder.setText(R.id.pageview, model.getBrowsingNum() + "人浏览");
        holder.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance()
                        .build("/comn/ComnQuestionItemInfoRUI")
                        .withString("id", model.getNid())
                        .withString("type","8")
                        .withString("commentRequestPath","Comment/getQuestionComment")
                        .withString("headRequestPath","Community/getQuestion")
                        .navigation();
            }
        });
    }

    @Override
    protected void onRestart() {
        onRefresh();
        super.onRestart();
    }

}
